/**
 * 
 */   
package com.cares.infobird.service;  

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.cares.core.base.exception.ApplyException;
import com.cares.core.base.service.BaseService;
import com.cares.core.mng.service.OrgService;
import com.cares.entity.base.PageModel;
import com.cares.entity.infobird.CallRecord;
import com.cares.infobird.repository.CallRecordMybatisDao;
import com.cares.infobird.util.HttpUtil;

/**   
 * @作者 Stone
 * @创建日期 2014-7-26 下午12:03:31
 * @版本 V1.0
 * @类说明
 */
@Service("callRecordService")
@Transactional(readOnly = true)
public class CallRecordService extends BaseService {

	@Autowired
	private CallRecordMybatisDao callRecordDao;
	
	@Resource(name="orgService")
	private OrgService orgService;
	
	
	/**
	 * 查询列表信息
	 * @param pageModel
	 * @return
	 */
	public String list(PageModel<CallRecord> pageModel) {

		String result = "";
		JSONObject json = new JSONObject();
		List<CallRecord> results = callRecordDao.getAll(pageModel);
		try {
			json.put("total", this.callRecordDao.count(pageModel));
			json.put("rows", JSON.toJSON(results));
			result = json.toString();
		} catch (JSONException e) {
		}
		return result;
	}
	
	/**
	 * 根据ID查询CallRecord
	 */
	public CallRecord get(Long id) {
		return callRecordDao.get(id);
	}
	
	/**
	 * 新增CallRecord
	 */
	public void save(CallRecord record) {
		record.setCreateTime(dateProvider.getDate());
		callRecordDao.save(record);
	}
	
	/**
	 * 更新CallRecord
	 */
	public void update(CallRecord record) {
		Assert.notNull(record.getId(), "缺少主键");
		record.setUpdateTime(dateProvider.getDate());
		callRecordDao.update(record);
	}
	
	/**
	 * 根据ID删除CallRecord
	 */
	public void delete(Long id) {
		callRecordDao.delete(id);
	}
	
	public void delete(List<String> ids) throws ApplyException {
		for (String id : ids) {
			delete(Long.parseLong(id));
		}
	}
	
	public CallRecord saveRecord(CallRecord record) {
		//根据callId查询record是否存在
		CallRecord dbRecord = callRecordDao.getRecord(record);
		if(dbRecord!=null && dbRecord.getId()!=null) {
			return dbRecord;
		} else {
			save(record);
			return record;
		}
	}
	
}
